$(function () {

    var selectedChat;


    GetChanels();
    //setInterval(GetChanels, 2000);
    setInterval(UpdateChat, 500);


    function UpdateChat() {


        if (selectedChat == null) {
            return;
        }
        console.log(selectedChat);
        var serviceUrl = "http://" + document.location.host + "/GetChanelMessageService.svc/GetMessages/" + 20 + "/" + selectedChat;
        console.log(serviceUrl);
        $.getJSON(serviceUrl, function (data) {
            console.log("sucess");
            var chanelList = $("#postList");
            var html = "";
            for (ch = data.length - 1; ch > -1; ch--) {
                var date = new Date(parseInt(data[ch].MessageTime.substr(6)));
                var t = data[ch].MessageText;
                var hourMinSec = '[' + date.getHours() + ':' + date.getMinutes() + ':' + date.getSeconds() + ']';
                html += "<li class='post'>" + hourMinSec + data[ch].MessageText + "</li>";
            }
            if (chanelList.html() != html) {
                chanelList.html(html);
            }
        }
        );

    }

    function GetChanels() {

        var serviceUrl = "http://" + document.location.host + "/GetChanelMessageService.svc/GetChanels";



        $.getJSON(serviceUrl, function (data) {
            if (selectedChat == null) {
                selectedChat = data[0];
            }
            var chanelList = $("#chanelList");
            var html = "";
            for (var ch in data) {
                if (selectedChat == data[ch]) {
                    html += "<li class='selected' id='" + ch + "'>" + data[ch] + "</li>";
                }
                else {
                    if (ch == (data.length - 1)) {
                        html += "<li class='last' id='" + ch + "'>" + data[ch] + "</li>";
                    }
                    else {
                        html += "<li id='" + ch + "'>" + data[ch] + "</li>";
                    }
                }

            }
            if (chanelList.html() != html) {
                chanelList.html(html);
            }
        });
    }

    $('#post').click(Post);
    function Post() {
        var serviceUrl = "http://" + document.location.host + "/GetChanelMessageService.svc/PostMessage";
        var post = new Object();
        post.Chanel = selectedChat;
        var text = document.getElementById('texField').value;
        document.getElementById('texField').value = null;
        post.Message = text;
        var postJson = JSON.stringify(post);
        $.ajax({
            type: 'POST',
            async: true,
            url: serviceUrl,
            data: postJson,
            datatype: 'JSON',
            contentType: "application/json; charset=utf-8"
        });
    }

    $('#texField').bind('keydown', function (e) {
        if (e.which == 13) {
            Post()
        }
    }
    );

    $('#chanelList li').live("click",
      function selectGame() {
          selectedChat = $(this).text()
          var selected = $('.selected');
          selected.removeClass('selected');
          var selected = $(this);
          selected.addClass('selected');
      });
    //    $('#chanelList').bind('click', function (e) {

    //        alert(e.currentTarget[1])
    //    });
});